package com.sq.imp;

import com.sq.abstractClass.Text;
import com.sq.imp.hash.FileHashCalculator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;

import static com.sq.imp.hash.FileHashCalculator.calculateFileHash;
import static com.sq.imp.hash.FileHashCalculator.calculateStringHash;

public class TextImp extends Text {
    public TextImp() {
        super();
        file = null;
        encoding = null;
        setLineWrap(true);
        setWrapStyleWord(true);
        upLabel = new UpLabelImp();
        lowLabel = new LowLabelImp();
        setFont(getFont());
        addTimer();
        addCaretListener();


    }



    @Override
    public void setFont(Font font) {
        super.setFont(font);
        if (upLabel != null) {
            upLabel.setFontString(font);
        }
    }
    @Override
    public void setEncoding(String encoding) {
        this.encoding = encoding;
        lowLabel.setEncoding(encoding);
    }

    @Override
    public void setFile(File file) {
        this.file = file;
        this.lastHash = null;
        if (file != null) {
            lowLabel.setFileName(file.getName());
        }else
            lowLabel.setFileName(null);
    }

    void addTimer() {
        Timer timer = new Timer();
        final int[] signal = {0};
        double frequency_minute = (double) 1 / 6;
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                upLabel.time.setText(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd|hh:mm:ss")));

                if ((LocalDateTime.now().getSecond() % (frequency_minute * 60)) == 1)
                    signal[0] = 1;
                if (signal[0] == 1 && (LocalDateTime.now().getSecond() % (frequency_minute * 60)) == 0 && getFile() != null) {
                    try {
                        String Hash = calculateFileHash(getFile().getPath(), FileHashCalculator.HashAlgorithm.SHA256);
                        System.out.println(Hash);

                        if (lastHash != null && !Objects.equals(lastHash, Hash)) {
                            if (!Objects.equals(calculateStringHash(getText(), FileHashCalculator.HashAlgorithm.SHA256), Hash)) {
                                upLabel.setFileIsChange("目标文件被其他程序篡改");
                            } else {
                                upLabel.setFileIsChange("目标文件发生改变");
                            }

                        }
                        lastHash = Hash;
                        signal[0] = 0;

                    } catch (IOException | NoSuchAlgorithmException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }, 0, 80);
    }
    @Override
    public void addCaretListener() {
        // 添加光标监听器
        addCaretListener(e -> {
            try {
                // 获取光标位置
                int offset = e.getDot();
                // 获取光标所在的行号
                int row = getLineOfOffset(offset);
                // 获取光标所在的列号
                int column = offset - getLineStartOffset(row);
                // 打印光标位置信息
                lowLabel.offset.setText("%d行%d列 %d/%d".formatted(column, row, getSelectedText() != null ? getSelectedText().length() : 0, getText() != null ? getText().length() : 0));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
    }

    @Override
    public File getFile() {
        return this.file;
    }

    @Override
    public String getEncoding() {
        return this.encoding;
    }
}
